package lc.smart.android.location;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class LocationInstance
{
	private LocationManager mLocationManager;
	private Context context;
	/**
	 * 纬度
	 * */
	private double mLatitude;
	/**
	 * 经度
	 * */
	private double mLongitude;

	//	getLatestLocation();
	// 注册LocationListener 时间间隔5秒 位移变化10米
	//		mLocationManager.requestLocationUpdates(getLocationProvicerCriteria(),
	//				5 * 1000, 10, ll);
	LocationListener ll = new LocationListener()
	{
		@Override
		public void onProviderEnabled(String provider)
		{
			System.out.println("Enabled");
			//			Location location = mLocationManager
			//					.getLastKnownLocation(provider);
			//			System.out
			//					.println("-----------------LocationListener-------location:"
			//							+ location);
			//			if (location != null)
			//			{
			//				mLatitude = location.getLatitude();
			//				mLongitude = location.getLongitude();
			//
			//			}
			//			else
			//			{
			//				Log.e(TAG, "---------------------------------location null");
			//			}
		}

		@Override
		public void onProviderDisabled(String provider)
		{
			System.out.println("disabled");

		}

		@Override
		public void onLocationChanged(Location location)
		{
			System.out.println("changed");
			//updateView(location);
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras)
		{
			System.out.println("statuschanged");
		}
	};

	/**
	 * 获取定位服务方式
	 * */
	public Criteria getLocationProvicerCriteria()
	{
		//		Log.v(TAG,
		//				"---------------------------------setLocationProvicerCriteria called!");
		Criteria locationProvicerCrtiteria = new Criteria();
		locationProvicerCrtiteria.setCostAllowed(false);
		locationProvicerCrtiteria.setAccuracy(Criteria.ACCURACY_FINE);
		locationProvicerCrtiteria.setAltitudeRequired(true);
		locationProvicerCrtiteria.setBearingRequired(true);
		locationProvicerCrtiteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
		locationProvicerCrtiteria.setSpeedRequired(true);
		//		String bestProvider = mLocationManager.getBestProvider(
		//				locationProvicerCrtiteria, true);

		//		Log.i(TAG, "-----------------" + bestProvider);
		return locationProvicerCrtiteria;

	}

	/**
	 * 获取定位信息
	 * */
	public void getLatestLocation()
	{
		//Log.v(TAG, "---------------------------------getLatestLocation called!");
		mLocationManager = (LocationManager) context
				.getSystemService(context.LOCATION_SERVICE);
		Location location = mLocationManager
				.getLastKnownLocation(mLocationManager.getBestProvider(
						getLocationProvicerCriteria(), true));
		//Log.d(TAG, "Location:" + location);
		if (location != null)
		{
			this.mLatitude = location.getLatitude();
			this.mLongitude = location.getLongitude();
			//进行数据格式化
			//setData();
			byte[] bytesDate = new byte[6];
			SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd-HH-mm-ss");
			String[] strings = String.valueOf(
					sdf.format(new Date(location.getTime())).toString()).split(
					"-");
			//Log.i(TAG, "--------" + sdf.format(new Date(location.getTime())));
			for (int i = 0; i < strings.length; i++)
			{
				//System.out.println(strings[i]);
				strings[i] = Integer.toHexString(Integer.valueOf(strings[i]));
				if (strings[i].length() < 2)
				{
					strings[i] = "0" + strings[i];
				}
				System.out.print(strings[i] + " ");
				//				bytesDate[i] = Constants.HexString2Bytes(strings[i])[0];
				//				Constants.bytesToSend[31 + i] = bytesDate[i];
			}
			System.out.println();
			System.out.println("--------------以16进制打印");
			for (int i = 0; i < bytesDate.length; i++)
			{
				if (i % 16 == 0) System.out.println();
				String strHex = new String();
				strHex = Integer.toHexString(bytesDate[i]).toUpperCase();
				if (strHex.length() > 3) System.out.print(strHex.substring(6));
				else if (strHex.length() < 2) System.out.print("0" + strHex);
				else System.out.print(strHex);
				System.out.print(" ");

				//System.out.print(" " + bytesToSend[i]);

			}
			System.out.println();
		}
		else
		{
			//Log.e(TAG, "---------------------------------location null");
		}
	}
}
